﻿using System;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using Nuwanda.TfsSpotlight.Shell.Proxies;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using StructureMap;

namespace Nuwanda.TfsSpotlight.Shell
{
    public partial class ShellView : KryptonForm
    {
        private ShellLayoutState _state;

        public ShellView()
        {
            InitializeComponent();
            this.LeftSideBar = new SideBarProxy(this.khgLeftSideBar, this.kscLeft, new ToolStripProxy(this.tsLeft), SideBars.Left);
            this.RightSideBar = new SideBarProxy(this.khgRightSideBar, this.kscRight, new ToolStripProxy(this.tsRight), SideBars.Right);
            this.ToolStrip = new ToolStripProxy(this.toolStrip1);
            this.MenuStrip = new MenuStripProxy(this.menuStrip1);
            this.StatusBar = new StatusBarProxy(this.statusStrip1);
            this.MonitBar = new KryptonHeaderProxy(this.khMonitBar);
            this.TabStrip = new TabStripProxy(this.faTabStrip1);
            this.KryptonManager = new KryptonManagerProxy(this.kryptonManager1);
            this.SaveFileDialog = new SaveFileDialogProxy(this.sfdSave);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        #region Getters

        public IToolStrip ToolStrip { get; private set; }
        public IMenuStrip MenuStrip { get; private set; }
        public IStatusBar StatusBar { get; private set; }
        public IKryptonHeader MonitBar { get; private set; }
        public ISideBar LeftSideBar { get; private set; }
        public ISideBar RightSideBar { get; private set; }
        public ITabStrip TabStrip { get; private set; }
        public IKryptonManager KryptonManager { get; set; }
        public ISaveFileDialog SaveFileDialog { get; set; }
        #endregion

        #region Shell layout state

        public ShellLayoutState GetCurrentLayoutState()
        {
            return new ShellLayoutState(
                this.Location, this.Size,
                this.WindowState,
                this.LeftSideBar.Width,
                this.LeftSideBar.Collapsed,
                this.RightSideBar.Width,
                this.RightSideBar.Collapsed);
        }

        public void ApplyLayoutState(ShellLayoutState layoutState)
        {
            this._state = layoutState;

            if (this._state == null)
            {
                this.StartPosition = FormStartPosition.CenterScreen;
                this._state = new ShellLayoutState(this.Location, this.Size, FormWindowState.Normal, 200, false, 200, false);
            }
            else
            {
                this.StartPosition = FormStartPosition.Manual;
            }

            if (!this._state.Location.IsEmpty)
                this.Location = this._state.Location;
            if (!this._state.Size.IsEmpty)
                this.Size = this._state.Size;

            this.WindowState = this._state.WindowState;

            this.LeftSideBar.Width = this._state.ProjectsPaneWith;
            this.LeftSideBar.Collapsed = this._state.ProjectPaneExpanded;
            this.RightSideBar.Width = this._state.HistoryPaneWith;
            this.RightSideBar.Collapsed = this._state.HistoryPaneExpanded;
        }
        #endregion

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            var menuService = ObjectFactory.GetInstance<IMenuService>();
            var command = menuService.GetCommandNameByShortcut(keyData);
            if (!String.IsNullOrEmpty(command))
            {
                menuService.RunCommand(command);
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
